package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.entity.ImportSubjectDTO;
import com.woniu.pojo.Subject;
import com.woniu.mapper.SubjectMapper;
import com.woniu.service.ISubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.TreeSubjectVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-14
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {
    @Resource
    SubjectMapper subjectMapper;

    @Override
    public List<Subject> listbyParentId() {
        return subjectMapper.listbyParentId();
    }

/*    @Override
    public void importSubject(List<ImportSubjectDTO> subjectDTOList) {
        //1.遍历集合
        //2，操作一级分类
        //2.1判断该一级分类在数据库中是否存在，如果不存在，则要存储一级分类(如果存在不需要进行处理)
        for (ImportSubjectDTO importSubjectDTO : subjectDTOList) {

            QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("title",importSubjectDTO.getOneSubject());
            queryWrapper.eq("parent_id",'0');
//            Subject one = this.getOne(queryWrapper);
            Subject onesubject = subjectMapper.selectOne(queryWrapper);
            if (onesubject==null){
                onesubject = new Subject();
                onesubject.setTitle(importSubjectDTO.getOneSubject());
                subjectMapper.insert(onesubject);
            }
            //3.操作二级分类
            //3.1判断二级分类在数据库中是否存在，如果不存在，存储二级分类，并且要指定一级分类的id,如果存在页也不处理
            queryWrapper.eq("title",importSubjectDTO.getTwoSubject());
            queryWrapper.eq("parent_id",onesubject.getId());//设置二级分类的父id
            Subject twoSubject = subjectMapper.selectOne(queryWrapper);
            if (twoSubject==null){
                twoSubject = new Subject();
                twoSubject.setTitle(importSubjectDTO.getTwoSubject());
                twoSubject.setParentId(onesubject.getId());
                subjectMapper.insert(twoSubject);
            }
        }
    }*/

    /**
     * 第二种写法:用sql写
     */

    @Override
    public void importSubject(List<ImportSubjectDTO> subjectDTOList) {
        //1.遍历集合
        //2，操作一级分类
        //2.1判断该一级分类在数据库中是否存在，如果不存在，则要存储一级分类(如果存在不需要进行处理)
        for (ImportSubjectDTO importSubjectDTO : subjectDTOList) {


//            Subject one = this.getOne(queryWrapper);
            Subject onesubject = subjectMapper.selectOneSubject(importSubjectDTO.getOneSubject());
            if (onesubject == null) {
                onesubject = new Subject();
                onesubject.setTitle(importSubjectDTO.getOneSubject());
                subjectMapper.insert(onesubject);
            }
            //3.操作二级分类
            //3.1判断二级分类在数据库中是否存在，如果不存在，存储二级分类，并且要指定一级分类的id,如果存在页也不处理
            Subject twoSubject = subjectMapper.selectTwoSubject(onesubject.getId(), importSubjectDTO.getTwoSubject());
            if (twoSubject == null) {
                twoSubject = new Subject();
                twoSubject.setTitle(importSubjectDTO.getTwoSubject());
                twoSubject.setParentId(onesubject.getId());
                subjectMapper.insert(twoSubject);
            }
        }
    }

    @Override
    public List<TreeSubjectVO> findSubjectTrees() {
        //1、先查出来一级分类的集合,返回的数据类型应为TreeSubjectVO
     /*  List<TreeSubjectVO> oneList = subjectMapper.listByParentId("0");*/

        //2、将集合中的元素遍历，用每个元素的id查出来二级分类的集合
/*
        long start = System.currentTimeMillis();
        for (TreeSubjectVO SubjectVO : oneList) {
         List<TreeSubjectVO> twoList = subjectMapper.listByParentId(SubjectVO.getId());
         //3、将集合赋值给一级分类的集合
         SubjectVO.setChildren(twoList);
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);
*/
        long start = System.currentTimeMillis();
        //1.查询出所有的分类数据
        List<TreeSubjectVO> subjectVOList = subjectMapper.selectAll();
        //2.操作stream流过滤出所有一级分类
        List<TreeSubjectVO> oneList =
                subjectVOList.stream().filter(subjectVO->subjectVO.getParentId().equals("0")).collect(Collectors.toList());
        //3.遍历该一级分类，将该一级分类对应的二级分类查询出阿里，并设置到children属性中
        for (TreeSubjectVO treeSubjectVO : oneList) {
            List<TreeSubjectVO> twoList =
                    subjectVOList.stream().filter(subjectVO->subjectVO.getParentId().equals(treeSubjectVO.getId())).collect(Collectors.toList());
                    treeSubjectVO.setChildren(twoList);
        }
        //4.返回
        long end = System.currentTimeMillis();


        System.out.println(end-start);

        return  oneList;

    }

    @Override
    public List<Subject> findsubjectByParentId(String parentId) {
        List<Subject> subjectList = subjectMapper.listByParentIdPlus(parentId);

        return subjectList;
    }


}
